package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.service.ServiceRegistrationService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.serviceregistration.pojo.dto.ServiceRegistrationAddDTO;
import com.xbongbong.pro.serviceregistration.pojo.dto.ServiceRegistrationDeleteDTO;
import com.xbongbong.pro.serviceregistration.pojo.dto.ServiceRegistrationGetDTO;
import com.xbongbong.pro.serviceregistration.pojo.dto.ServiceRegistrationListDTO;
import com.xbongbong.pro.serviceregistration.pojo.dto.ServiceRegistrationUpdateDTO;
import com.xbongbong.pro.serviceregistration.pojo.vo.ServiceRegistrationAddVO;
import com.xbongbong.pro.serviceregistration.pojo.vo.ServiceRegistrationDeleteVO;
import com.xbongbong.pro.serviceregistration.pojo.vo.ServiceRegistrationGetVO;
import com.xbongbong.pro.serviceregistration.pojo.vo.ServiceRegistrationListVO;
import com.xbongbong.pro.serviceregistration.pojo.vo.ServiceRegistrationUpdateVO;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2021/03/03 17:18
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/serviceRegistration")
public class ServiceRegistrationController {


    @Resource
    private ServiceRegistrationService serviceRegistrationService;

    /**
     * 列表
     *
     * @param serviceRegistrationListDTO
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String list(@RequestBody @Valid ServiceRegistrationListDTO serviceRegistrationListDTO, BindingResult br) throws Exception {
        XbbResponse<ServiceRegistrationListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ServiceRegistrationListVO serviceRegistrationListVO = serviceRegistrationService.list(serviceRegistrationListDTO);
                response = new XbbResponse<>(serviceRegistrationListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
    /**
     * get
     *
     */
    @RequestMapping(value = "/get", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String add(@RequestBody @Valid ServiceRegistrationGetDTO serviceRegistrationGetDTO, BindingResult br) throws Exception {
        XbbResponse<ServiceRegistrationGetVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ServiceRegistrationGetVO serviceRegistrationAddVO = serviceRegistrationService.get(serviceRegistrationGetDTO);
                response = new XbbResponse<>(serviceRegistrationAddVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 新建
     *
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String add(@RequestBody @Valid ServiceRegistrationAddDTO serviceRegistrationAddDTO, BindingResult br) throws Exception {
        XbbResponse<ServiceRegistrationAddVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ServiceRegistrationAddVO serviceRegistrationAddVO = serviceRegistrationService.add(serviceRegistrationAddDTO);
                response = new XbbResponse<>(serviceRegistrationAddVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 更新
     *
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String update(@RequestBody @Valid ServiceRegistrationUpdateDTO serviceRegistrationUpdateDTO, BindingResult br) throws Exception {
        XbbResponse<ServiceRegistrationUpdateVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ServiceRegistrationUpdateVO serviceRegistrationUpdateVO = serviceRegistrationService.update(serviceRegistrationUpdateDTO);
                response = new XbbResponse<>(serviceRegistrationUpdateVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 删除
     *
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String delete(@RequestBody @Valid ServiceRegistrationDeleteDTO serviceRegistrationDeleteDTO, BindingResult br) throws Exception {
        XbbResponse<ServiceRegistrationDeleteVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ServiceRegistrationDeleteVO serviceRegistrationDeleteVO = serviceRegistrationService.delete(serviceRegistrationDeleteDTO);
                response = new XbbResponse<>(serviceRegistrationDeleteVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

}
